function init() {
  chrome.runtime.onMessage.addListener(async (req, sender, sendResponse) => {
    sendResponse("哈哈哈");
    const tabId = await getCurrentTabId();
    chrome.tabs.sendMessage(
      tabId,
      "我是background，我在发送消息",
      function (res) {
        console.log("background：", res);
      }
    );
  });
}

/**
 * 获取当前 tab ID
 */
function getCurrentTabId() {
  return new Promise((resolve, reject) => {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
      resolve(tabs.length ? tabs[0].id : null);
    });
  });
}

function send(message) {
  return new Promise(async (resolve) => {
    const tabId = await getCurrentTabId();
    chrome.tabs.sendMessage(tabId, message, (response) => {
      resolve(response);
    });
  });
}

export default {
  init,
  send,
};
